import {
	slTip
} from "./tip.js";
import {
	getToken
} from "./auth.js";
import store from '../store/index.js'
/**
 * @description: loading
 * @param: String
 * @returns: Promise
 */
export function slNavigateTo(url = '') { // 默认是navigateTo方式
	if (!url) {
		slTip('请传入地址');
		return
	}
	if (!getToken() && (Object.keys(store.state.userInfo).length == 0)) {
		uni.navigateTo({
			url: '/pages/login/index'
		})
		return;
	}
	uni.navigateTo({
		url
	})
};

/**
 * @description: 计算两点距离
 * @param: number
 * @returns: number
 */
// 经纬度转换成三角函数中度分表形式。
function rad(d) {
	return d * Math.PI / 180.0;
}

// 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
export function getDistance(lat1, lng1, lat2, lng2) {

	var radLat1 = rad(lat1);
	var radLat2 = rad(lat2);
	var a = radLat1 - radLat2;
	var b = rad(lng1) - rad(lng2);
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; //输出为公里

	var distance = s;
	var distance_str = "";

	if (parseInt(distance) >= 1) {
		distance_str = distance.toFixed(1) + "km";
	} else {
		distance_str = distance * 1000 + "m";
	}

	//s=s.toFixed(4);
	return distance_str;
}
